package cn.lyq.card.ui.common

import androidx.core.content.ContextCompat
import cn.lyq.card.R
import com.blankj.utilcode.util.SpanUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder


class KeyAdapter : BaseQuickAdapter<String, BaseViewHolder>(
    R.layout.item_bank, mutableListOf()
), LoadMoreModule {

    var key = ""
    var isShowArrow = true

    override fun convert(holder: BaseViewHolder, item: String) {
        if (key.isEmpty() || !item.contains(key)) {
            holder.setText(R.id.tvName, item)
        } else {
            val temp = item.split(key)
            val utils = SpanUtils.with(holder.getView(R.id.tvName))
            temp.withIndex().forEach {
                if (it.value.isNotEmpty()) {
                    utils.append(it.value)
                }
                if (it.index != temp.size - 1) {
                    utils.append(key)
                        .setForegroundColor(ContextCompat.getColor(context, R.color.font_blue))
                }
            }
            utils.create()
        }
        holder.setVisible(R.id.imgArrow, isShowArrow)
        holder.setVisible(R.id.tvShadow, holder.adapterPosition != data.size)
    }
}